import pymysql


class MyORM:
    def __init__(self):
        conn = pymysql.connect(
            host='127.0.0.1',
            port=3306,
            user='root',
            password='123456',
            database='mumushouji',
            charset='utf8',
            autocommit=True,
            cursorclass=pymysql.cursors.DictCursor
        )
        cursor = conn.cursor()
        self.cursor = cursor

    def execute(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()


class User:
    table_name = 'user'

    def query_all(self):
        sql = 'select * from %s limit 2' % self.table_name
        return MyORM().execute(sql)

    def query_one(self):
        sql = 'select * from %s limit 1' % self.table_name
        return MyORM().execute(sql)


class Article:
    table_name = 'article'

    def query_all(self):
        sql = 'select * from %s limit 2' % self.table_name
        return MyORM().execute(sql)

    def query_one(self):
        sql = 'select * from %s limit 1' % self.table_name
        return MyORM().execute(sql)


if __name__ == '__main__':
    from icecream import ic

    user = User()
    r = user.query_all()
    ic(r)
    article = Article()
    r2 = article.query_all()
    ic(r2)
    ic(user.query_one())
    ic(article.query_one())
